<template>
    <div class="trends">
        <!-- search -->
        <div class="search">
            <input type="text" @focus="inputFocus" @blur="inputBlur" v-model="searchInput" class="searchInput" />
            <i class="search-icon" v-show="searchShow"></i>
            <span class="searchPlaceHolder" v-show="searchShow">搜索</span>
        </div>
        <mTitle :title="title"></mTitle>
        <scroll :data="hotspotList" class="trends-wrapper" :pullup="pullup" @scrollToEnd="searchMore" v-show="!searchInput">
            <div v-show="hotspotList.length">
                <ul class="trends-item">
                    <router-link :to="'/detailNews/' + item.id" tag="li" v-for="(item, index) in hotspotList" :key="item.id">
                        <div class="left">
                            <img class="trends-img" v-lazy="item.coverimg" />
                        </div>
                        <div class="right">
                            <h3>{{item.title}}</h3>
                            <span class="trends-date">{{item.pubdate}}</span>
                            <p class="trends-desc">{{item.content.substr(0,36)}}...</p>
                        </div>
                    </router-link>
                    <loading v-show="!hasMore"></loading>
                    <li class="bottom-text" v-show="endMore">已加载全部数据</li>
                </ul>
            </div>
            <!-- 请求数据loading -->
            <div class="loading-wrapper" v-show="!hotspotList.length">
                <loading></loading>
            </div>
        </scroll>
        <!-- 搜索结果展示 -->
        <scroll :data="list" v-show="list.length" class="trends-wrapper">
            <ul class="trends-item">
                <router-link :to="'/detailNews/' + item.id" tag="li" v-for="(item, index) in list" :key="item.id">
                    <div class="left">
                        <img class="trends-img" :src="item.coverimg" />
                    </div>
                    <div class="right">
                        <h3>{{item.title}}</h3>
                        <span class="trends-date">{{item.pubdate}}</span>
                        <p class="trends-desc">{{item.content.substr(0,36)}}...</p>
                    </div>
                </router-link>
            </ul>
        </scroll>
        <div class="no-result" v-show="!list.length && searchInput">暂无搜索结果</div>
    </div>
</template>

<script>
    import axios from 'axios'
    import mTitle from 'base/title/title'
    import Scroll from 'base/scroll/scroll'
    import Loading from 'base/loading/loading'
    export default {
        data () {
            return {
                searchInput: "",
                searchShow: true,
                title: '省内动态',
                hotspotList: [],
                searchList: [],
                hasMore: true,
                endMore: false,
                pullup: true,
                list: [],
                num: 1,
                size: 10,
                total: 0
            }
        },
        methods: {
            // 获取焦点
            inputFocus () {
                this.searchShow = false
                if (this.searchList.length > 0) {
                    return
                } else {
                    axios.get('/api/index.php?g=Api&m=Guanwang&a=shengnei&page='+ this.num +'&size=' + this.total +'').then((res) => {
                        res = res.data
                        this.searchList = res.list
                    })
                }
            },
            // 失去焦点
            inputBlur () {
                if (this.searchInput === "") {
                    this.searchShow = true
                }
            },
            // 加载数据
            getHotSpotInfo () {
                axios.get('/api/index.php?g=Api&m=Guanwang&a=shengnei&page='+ this.num +'&size=' + this.size +'').then((res) => {
                    res = res.data
                    this.total = res.total
                    this.hasMore = false
                    this.hotspotList = res.list
                })
            },
            // 下拉加载
            searchMore () {
                if (this.total < 10) {
                    this.hasMore = true
                    this.endMore = true
                    return
                }
                this.num = this.num+1
                if ((this.num-1) * this.size <= this.total) {
                    axios.get('/api/index.php?g=Api&m=Guanwang&a=shengnei&page='+ this.num +'&size=' + this.size +'').then((res) => {
                        let data = res.data.list
                        if (!data.length) {
                            this.hasMore = true
                        }
                        this.hotspotList = this.hotspotList.concat(data)
                    })
                } else {
                    this.hasMore = true
                    this.endMore = true
                } 
            }
        },
        watch: {
            searchInput () {
                this.list = []
                let result = []
                if(this.searchInput === "") {
                    result = []
                    this.list = []
                    return
                }
                for (let i in this.searchList) {
                    let value = this.searchList[i].title
                    if(value.indexOf(this.searchInput) > -1) {
                        result.push(i)
                    }
                }
                for(let k in result) {
                    this.list = this.list.concat(this.searchList[result[k]])
                }
            }
        },
        components: {mTitle,Scroll,Loading},
        created () {
            this.getHotSpotInfo()
        }
    }
</script>

<style lang="stylus" scoped>
    .trends
        width: 100%
        height: 100%
        position: relative
        overflow: hidden
        background: #fff
        .search
            width: 100%
            height: 44px
            background: #efeff4
            padding: 0 15px
            box-sizing: border-box
            -webkit-box-sizing: border-box
            position: relative
            overflow: hidden
            .searchInput
                width: 100%
                height: 32px
                background: #fff
                margin-top: 6px
                border: none
                border-radius: 0
                padding: 0 10px
                box-sizing: border-box
                -webkit-box-sizing: border-box
            .search-icon
                position: absolute
                background: url('./search-icon.png') no-repeat
                width: 13px
                height: 13px
                background-size: 100% 100%
                top: 16px
                left: 50%
                margin-left: -15px
            .searchPlaceHolder
                font-size: 14px
                color: #979797
                position: absolute
                top: 17px
                left: 51%
        .no-result
            padding: 0 15px
            line-height: 30px
            font-size: 16px
        .trends-wrapper
            position: absolute
            overflow: hidden
            top: 86px
            left: 0
            right: 0
            bottom: 0px
            box-sizing: border-box
            width: 100%
            .loading-wrapper
                position: absolute
                top: 50%
                transform: translateY(-50%)
                width: 100%
            .trends-item
                width: 100%
                padding: 0 15px
                box-sizing: border-box
                -webkit-box-sizing: border-box
                li
                    width: 100%
                    border-bottom: 1px solid #f2f2f2
                    display: flex
                    padding: 10px 0
                    align-items: center
                    &.bottom-text
                        width: 100%
                        height: 40px
                        line-height: 40px
                        font-size: 14px
                        text-align: center
                        color: #ddd
                        justify-content: center
                        padding: 0
                        border-bottom: none
                    .left
                        width: 125px
                        height: 95px
                        flex: 0 0 125px
                        img
                            width: 100%
                            height: 100%
                    .right
                        flex: 1
                        margin-left: 14px
                        position: relative
                        h3
                            font-size: 14px
                            font-weight: 700
                            line-height: 140%
                            color: #000
                        .trends-date
                            display: block
                            color: #888
                            font-size: 12px
                            margin-top: 6px
                        .trends-desc
                            color: #888
                            font-size: 12px
                            margin-top: 6px
                            line-height: 150%                       
</style>